package com.kkb.shopcenter.server.impl;

import cn.hutool.core.date.DateUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kkb.shopcenter.api.OrderService;
import com.kkb.shopcenter.common.constant.ConstantKey;
import com.kkb.shopcenter.common.dto.ChartDataDto;
import com.kkb.shopcenter.common.dto.OrderCount;
import com.kkb.shopcenter.common.dto.OrderCountData;
import com.kkb.shopcenter.common.dto.OrderTimeDataDto;
import com.kkb.shopcenter.common.pojo.*;
import com.kkb.shopcenter.common.utils.DateTimeUtil;
import com.kkb.shopcenter.server.mappers.*;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.util.*;

@Service
public class OrderServiceImpl implements OrderService {


    @Autowired
    private SubOrderInfoMapper subOrderInfoMapper;

    @Autowired
    private OrderItemMapper orderItemMapper;


    @Autowired
    private OrderDeliveryMapper orderDeliveryMapper;

    @Autowired
    private MemberInfoMapper memberInfoMapper;

    @Autowired
    private GoodsInfoMapper goodsInfoMapper;

    /**
     * 查询子订单列表
     * @param shopId  商家ID
     * @param orderStatus 订单状态
     * @param page 页码
     * @param size 每页条数
     * @return
     */
    @Override
    public PageInfo<SubOrderInfo> queryPage(Long shopId, Integer orderStatus, Integer page, Integer size) {
        PageHelper.startPage(page+1,size);
        Example example = new Example(SubOrderInfo.class);
        if(shopId!=null&&shopId>0){
            example.createCriteria().andEqualTo("shopId",shopId);
        }
        if(orderStatus!=null&&orderStatus>0){
            example.createCriteria().andEqualTo("orderStatus",orderStatus);
        }

        List<SubOrderInfo> subOrderInfos = subOrderInfoMapper.selectByExample(example);

        PageInfo<SubOrderInfo> pageInfo = new PageInfo<>(subOrderInfos);

        return pageInfo;
    }


    /**
     * 查询子订单包含商品列表
     * @param subOrderId 子订单ID
     * @return
     */
    @Override
    public List<OrderItem> getDetailInfo(Long subOrderId) {

        Example example = new Example(OrderItem.class);
        example.createCriteria().andEqualTo("subOrderId",subOrderId);
        return orderItemMapper.selectByExample(example);

    }

    /**
     * 查询自订单配送信息
     * @param subOrderId 子订单ID
     * @return
     */
    @Override
    public OrderDelivery getDeliverInfo(Long subOrderId) {
        Example example = new Example(OrderDelivery.class);
        example.createCriteria().andEqualTo("subOrderId",subOrderId);
        return orderDeliveryMapper.selectOneByExample(example);
    }

    /**
     * 查询所有子订单
     * @return
     */
    @Override
    public List<SubOrderInfo> selectAll() {
        return subOrderInfoMapper.selectAll();
    }

    @Override
    public OrderTimeDataDto orderTimeDate(Long shopId) {
        int today = DateTimeUtil.dateToTimestampT(DateUtil.beginOfDay(new Date()));
        int tomorrow = DateTimeUtil.dateToTimestampT(DateUtil.endOfDay(new Date()));
        int yestart =  DateTimeUtil.dateToTimestampT(DateUtil.beginOfDay(DateUtil.yesterday()));

        int lastWeek = DateTimeUtil.dateToTimestampT(DateUtil.beginOfDay(DateUtil.lastWeek()));

        int nowMoth = DateTimeUtil.dateToTimestampT(DateUtil.beginOfMonth(new Date()));

        OrderTimeDataDto orderTimeDataDto = new OrderTimeDataDto();

        //今日订单数
        orderTimeDataDto.setTodayCount(subOrderInfoMapper.countBetweenTime(shopId,today,tomorrow));

        //昨日订单数
        orderTimeDataDto.setProCount(subOrderInfoMapper.countBetweenTime(shopId,yestart,today));

        //本周(近7天)订单数

        orderTimeDataDto.setLastWeekCount(subOrderInfoMapper.countBetweenTime(shopId,lastWeek,tomorrow));

        //本月订单数
        orderTimeDataDto.setMonthCount(subOrderInfoMapper.countBetweenTime(shopId,nowMoth,tomorrow));

        Example exampleMember = new Example(MemberInfo.class);

        //会员数量
        orderTimeDataDto.setUserCount(memberInfoMapper.selectCountByExample(exampleMember));

        Example example1 = new Example(SubOrderInfo.class);
        example1.selectProperties("orderId").setDistinct(true);
        if(shopId>0){
            example1.createCriteria().andEqualTo("shopId",shopId);
        }

        int count = subOrderInfoMapper.selectCountByExample(example1);

        //订单总数
        orderTimeDataDto.setOrderCount(count);

        //订单总价
        orderTimeDataDto.setPriceCount(subOrderInfoMapper.sumTotalPrice(shopId));

        //商品总数
        Example example = new Example(GoodsInfo.class);
        orderTimeDataDto.setGoodsCount(goodsInfoMapper.selectCountByExample(example));

        return orderTimeDataDto;
    }

    @Override
    public OrderCount getOrderCount(Long shopId) {

        OrderCount orderCount = new OrderCount();
        List<String> columns = new ArrayList<>();
        List<OrderCountData> orderCountDataList = subOrderInfoMapper.selectOrderStatus(shopId);

        for (OrderCountData count:orderCountDataList){
            if(ConstantKey.UNPAID.equals(count.getName())){
                count.setName("未支付");
            }else if(ConstantKey.INVALID_ORDER.equals(count.getName())){
                count.setName("订单失效");
            }else if(ConstantKey.SEAL_THE_DEAL.equals(count.getName())){
                count.setName("完成交易");
            }else if(ConstantKey.MERCHANDISE_RETURN.equals(count.getName())){
                count.setName("商品退货");
            }
            columns.add(count.getName());
        }
        orderCount.setColumn(columns);
        orderCount.setOrderCountDatas(orderCountDataList);
        return orderCount;
    }

    @Override
    public Map<String, Object> chartCount(Long shopId) {
        Map<String,Object> map = new LinkedHashMap<>();
        int start = DateTimeUtil.dateToTimestampT(DateUtil.beginOfMonth(new Date()));
        int end = DateTimeUtil.dateToTimestampT(DateUtil.endOfMonth(new Date()));

        List<ChartDataDto> chartDataDtos = subOrderInfoMapper.charList(shopId,start,end);
        List<ChartDataDto> chartTDataDtos = subOrderInfoMapper.charListT(shopId,start,end);

        map.put("chart",chartDataDtos);
        map.put("chartT",chartTDataDtos);

        return map;
    }


}
